using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DTO;

namespace DAO
{
    public class BenhNhanDAO
    {
        public static bool Delete(BenhNhanDTO bn)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();

                sqlParams.Add(new SqlParameter("@MaBenhNhan", bn.MaBenhNhan));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("spBenhNhanDelete", sqlParams);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static bool Insert(BenhNhanDTO bn)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaBenhNhan", bn.MaBenhNhan));
                sqlParams.Add(new SqlParameter("@HoTen", bn.HoTen));
                sqlParams.Add(new SqlParameter("@GioiTinh", bn.GioiTinh));
                sqlParams.Add(new SqlParameter("@NamSinh", bn.NamSinh));
                sqlParams.Add(new SqlParameter("@DiaChi", bn.DiaChi));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("spBenhNhanInsert", sqlParams);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static bool UpDate(BenhNhanDTO bn)
        {
            bool kq = false;
            try
            {
                List<SqlParameter> sqlParams = new List<SqlParameter>();
                sqlParams.Add(new SqlParameter("@MaBenhNhan",bn.MaBenhNhan));
                sqlParams.Add(new SqlParameter("@HoTen", bn.HoTen));
                sqlParams.Add(new SqlParameter("@GioiTinh", bn.GioiTinh));
                sqlParams.Add(new SqlParameter("@NamSinh", bn.NamSinh));
                sqlParams.Add(new SqlParameter("@DiaChi", bn.DiaChi));

                int n = SqlDataAccessHelper.ExecuteNoneQuery("spBenhNhanUpdate", sqlParams);
                if (n == 1)
                    kq = true;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return kq;
        }
        public static List<BenhNhanDTO> Select()
        {
            List<BenhNhanDTO> List = new List<BenhNhanDTO>();
            try
            {
                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spBenhNhanSelect");
                foreach (DataRow dr in dt.Rows)
                {
                    BenhNhanDTO bn = new BenhNhanDTO();
                    bn.MaBenhNhan = int.Parse(dr["MaBenhNhan"].ToString());
                    bn.HoTen = (dr["HoTen"].ToString());
                    bn.GioiTinh = (dr["GioiTinh"].ToString());
                    bn.NamSinh = int.Parse(dr["NamSinh"].ToString());
                    bn.DiaChi = (dr["DiaChi"].ToString());

                    List.Add(bn);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return List;
        }
        public static BenhNhanDTO SelectByID(int MaBenhNhan)
        {
            BenhNhanDTO bn = new BenhNhanDTO();
            try
            {
                List<SqlParameter> sqlParam = new List<SqlParameter>();
                sqlParam.Add(new SqlParameter("@MaBenhNhan",MaBenhNhan));

                DataTable dt = SqlDataAccessHelper.ExecuteQuery("spBenhNhanSelectByID",sqlParam);
                DataRow dr = dt.Rows[0];

                bn.MaBenhNhan = int.Parse(dr["MaBenhNhan"].ToString());
                bn.HoTen = (dr["HoTen"].ToString());
                bn.GioiTinh = (dr["GioiTinh"].ToString());
                bn.NamSinh = int.Parse(dr["NamSinh"].ToString());
                bn.DiaChi = (dr["DiaChi"].ToString());
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return bn;
        }
    }
}
